package com.xxx.video.channel.presenters;

import android.content.Context;
import android.text.TextUtils;

import androidx.annotation.NonNull;

import com.google.gson.Gson;
import com.xxx.video.commen.configs.Constant;
import com.xxx.video.commen.presenters.BasePresenter;
import com.xxx.video.channel.beans.ChanelHomeListBean;
import com.xxx.video.commen.utils.ListUtils;
import com.xxx.video.commen.utils.ResponseUtils;
import com.xxx.video.commen.utils.SPUtil;
import com.xxx.video.main.beans.IndexBean;
import com.xxx.video.network.listeners.CommonResponseListener;
import com.xxx.video.network.utils.NetWorkUtils;

public class ChannelPresenter extends BasePresenter<IChannelView> {

    public ChannelPresenter(@NonNull Context context, IChannelView baseView) {
        super(context, baseView);
    }

    /**
     * get channel info
     */
    public void getChannelInfo() {
        NetWorkUtils.getInstance().getChannelInfo(new CommonResponseListener<ChanelHomeListBean>() {
            @Override
            public void onSuccess(ChanelHomeListBean successOb, String result) {
                if (ResponseUtils.getInstance().isNoError(successOb)) {
                    if (getBaseView() != null) {
                        if (!ListUtils.isEmpty(successOb.hotList) && !ListUtils.isEmpty(successOb.typeList)) {
                            SPUtil.putString(Constant.ChannelInfo, new Gson().toJson(successOb));
                        }
                        getBaseView().initData2View(successOb);
                    }
                }
            }

            @Override
            public void onFailure(Object failureObj) {
                if (getBaseView() != null) {
                    String result = SPUtil.getString(Constant.ChannelInfo, "");
                    if (!TextUtils.isEmpty(result)) {
                        try {
                            ChanelHomeListBean successOb = new Gson().fromJson(result, ChanelHomeListBean.class);
                            getBaseView().initData2View(successOb);
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    } else {
                        getBaseView().showRootLayoutStatus(3);
                    }
                }
            }
        });

    }
}
